package com.itheima.web;

import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.Page;
import com.itheima.pojo.PageResult;
import com.itheima.pojo.QueryPageBean;
import com.itheima.pojo.Result;
import com.itheima.pojo.Role;
import com.itheima.service.ManagerRoleService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * @version V1.0
 * @author: TXT
 * @date: 2020/2/12 9:52
 * @description: 角色管理
 */
@RestController
@RequestMapping("managerRole")
public class ManagerRoleController {

    @Reference
    private ManagerRoleService managerRoleService;

    @PreAuthorize("hasAuthority('ROLE_QUERY')")
    @RequestMapping("/findAll")
    @ResponseBody
    public Result findAll(){
        try {
            List<Role> roles = managerRoleService.findAll();
            return Result.success(true,"查询所有用户成功",roles);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(false,"查询所有用户失败");
        }
    }
    @PreAuthorize("hasAuthority('ROLE_QUERY')")
    @RequestMapping("findPage")
    @ResponseBody
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult page = null;
        try {
            page = managerRoleService.findPage(queryPageBean);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return page;
    }

    /**
     * 查询 权限,菜单
     * @return
     */
    @PreAuthorize("hasAuthority('ROLE_QUERY')")
    @RequestMapping("findPermissionAndMenu")
    @ResponseBody
    public Result findPermissionAndMenu(){
        try {
            Map map = managerRoleService.findPermissionAndMenu();
            return Result.success(true,"查询权限,菜单信息成功",map);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(false,"查询权限,菜单信息失败");
        }


    }

    /**
     * 查询 权限,菜单 ,根据角色id 查询对应的 权限id 菜单id
     * @param id
     * @return
     */
    @PreAuthorize("hasAuthority('ROLE_QUERY')")
    @RequestMapping("findPermIdsAndMenuIds")
    @ResponseBody
    public Result findPermIdsAndMenuIds(Integer id){
        try {
            Map map = managerRoleService.findPermIdsAndMenuIds(id);
            return Result.success(true,"查询角色权限,菜单信息成功",map);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(false,"查询角色权限,菜单信息失败");
        }
    }
    @PreAuthorize("hasAuthority('ROLE_ADD')")
    @RequestMapping("addMenuAndPerm")
    @ResponseBody
    public Result addMenuAndPerm(@RequestBody Role role){
        try {
            managerRoleService.addMenuAndPerm(role);
            return Result.success(true,"添加角色成功");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(false,"添加角色失败");
        }
    }
    @PreAuthorize("hasAuthority('ROLE_EDIT')")
    @RequestMapping("editMenuAndPerm")
    @ResponseBody
    public Result editMenuAndPerm(@RequestBody Role role){
        try {
            managerRoleService.editMenuAndPerm(role);
            return Result.success(true,"修改角色成功");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(false,"修改角色失败");
        }
    }
    @PreAuthorize("hasAuthority('ROLE_DELETE')")
    @RequestMapping("deleteRole")
    @ResponseBody
    public Result deleteRole(Integer id){
        try {
            boolean b = managerRoleService.deleteRole(id);
            if(b == false){
                return Result.error(false,"该角色被引用");
            }
            return Result.success(true,"删除角色成功");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(false,"删除角色失败");
        }
    }
}
